package org.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.book.bean.Borrow;
import org.book.bean.User;
import org.book.common.JsonResult;
import org.book.configuration.aop.PermissionCheck;
import org.book.service.IBookService;
import org.book.service.IBorrowService;
import org.book.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-03-05
 */

@Slf4j
@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    IBorrowService borrowService;

    @Autowired
    IUserService userService;

    @PostMapping("/listall")
    @PermissionCheck
    public JsonResult list(String key,Integer pageNum,Integer pageSize){

        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=0;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;
        try{
            QueryWrapper<Borrow> wrapper = Wrappers.query();
            if(key!=null&&!key.equals("")){
                wrapper.like("book.isbn" ,key).or().like("name",key).or().like("username",key);
            }
            page = new PageInfo<Borrow>(borrowService.listBorrow(wrapper));
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);

    }
    @PostMapping("/listmyself")
    public JsonResult listMysekf(String username,Integer pageNum, Integer pageSize ,HttpServletRequest request){
        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=0;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;
        try{
            QueryWrapper wrapper = Wrappers.query();
            wrapper.eq("username",username);
            page = new PageInfo<Borrow>(borrowService.listBorrow(wrapper));
        }catch (Exception e){
            return  JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/findById")
    public JsonResult findById(String isbn){
        Borrow borrow = null;
        try{
            borrow = borrowService.getById(isbn);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError(e.getStackTrace().toString());
        }
        return JsonResult.renderSuccess(borrow);
    }

    @PostMapping("/search")
    public JsonResult search(String key,Integer pageNum,Integer pageSize ) {


        if (pageSize != null && pageSize != 0) {
            if (pageNum == null) {
                pageSize = 0;
            }
            PageHelper.startPage(pageNum, pageSize);
        }
        PageInfo page = null;
        try {
            QueryWrapper wrapper = Wrappers.query();
            wrapper.like("isbn" ,key);
            wrapper.like("name",key);
            page = new PageInfo<Borrow>(borrowService.list(wrapper));
        } catch (Exception e) {
            return JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/insert")
    public JsonResult insert(Borrow borrow, HttpServletRequest request){
        try{
            User user = userService.getById(borrow.getUsername());
            if (user==null){
                return JsonResult.renderFail("借书失败");
            }
//            borrow.setUsername(request.getSession().getAttribute("user").toString());
            borrow.setBorrowdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            borrow.setBorrowid(UUID.randomUUID().toString().replace("-",""));
            borrowService.borrowbook(borrow);
            return JsonResult.renderSuccess("借书成功");
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderFail("借书失败");
        }

    }

    @PostMapping("/update")
    public JsonResult update(Borrow borrow){
        try{
            borrowService.retturnbook(borrow);
            return JsonResult.renderSuccess("还书成功");
        }catch (Exception e){
            return JsonResult.renderFail("还书失败");
        }

    }
    @PostMapping("/delete")
    public JsonResult delete(Borrow borrow){
        try{
            borrowService.removeById(borrow.getBorrowid());
            return JsonResult.renderSuccess("删除成功");
        }catch (Exception e){
            return JsonResult.renderFail("删除失败");
        }

    }

}
